package com.xhwl.data.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.dto.cent.sdata.AccountDetailDTO;
import com.xhwl.common.dto.cent.sdata.StaffAuditRecordDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Enterprise;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.pojo.cent.sdata.personnel.StaffAuditRecord;
import com.xhwl.common.query.cent.sdata.EnterpriseQuery;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.query.cent.sdata.StaffAuditRecordQuery;
import com.xhwl.data.pojo.dto.personal.StaffQueryPageDTO;
import com.xhwl.data.pojo.personal.StaffFieldMap;
import com.xhwl.data.service.IEnterpriseService;
import com.xhwl.data.service.IProjectService;
import com.xhwl.data.service.personal.IStaffAuditRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Author lanminjun
 * @Description: 入驻企业员工申请
 * @Date Create in 13:49 2023/02/15
 */
@RestController
public class StaffAuditRecordController {

    @Autowired
    private IStaffAuditRecordService staffAuditRecordService;
    @Autowired
    private IEnterpriseService enterpriseService;
    @Autowired
    private IProjectService projectService;

    /**
     * 提交申请/修改申请
     * @param staffAuditRecordDTO
     * @param account
     * @return
     */
    @RequestMapping(value = "/staff-audit/create")
    public ResultJson create(@RequestBody StaffAuditRecordDTO staffAuditRecordDTO, Account account){
        if(null == account){
            return ResultJson.fail("获取登录信息失败");
        }
        if (null == staffAuditRecordDTO.getAccountId()){
            staffAuditRecordDTO.setAccountId(account.getId());
        }
        if(null != staffAuditRecordDTO.getId()){
            return staffAuditRecordService.edit(staffAuditRecordDTO, account);
        }else{
            return staffAuditRecordService.create(staffAuditRecordDTO, account);
        }
    }

    /**
     * 分页查询
     * @param staffAuditRecordQuery
     * @return
     */
    @RequestMapping(value = "/staff-audit/page")
    public ResultJson page(@RequestBody StaffAuditRecordQuery staffAuditRecordQuery, Account account) {
        if (null == account){
            return ResultJson.fail("获取登录信息失败");
        }
        if (null == staffAuditRecordQuery.getAccountId()){
            staffAuditRecordQuery.setAccountId(account.getId());
        }
        staffAuditRecordQuery.setIsDeleted(false);
        Page page =  new Page();
        Page<StaffAuditRecord> staffAuditRecordPage = staffAuditRecordService.page(staffAuditRecordQuery,page);
        List<StaffAuditRecord> records = staffAuditRecordPage.getRecords();
        if (!records.isEmpty()){
            Map<Integer,String> enterpriseName = enterpriseHandler(records);
            Map<Integer,String> projectName = projectHandler(records);
            for (StaffAuditRecord staffAuditRecord : records){
                staffAuditRecord.setEnterpriseName(enterpriseName.get(staffAuditRecord.getEnterpriseId()));
                staffAuditRecord.setProjectName(projectName.get(staffAuditRecord.getProjectId()));
            }
        }
        return ResultJson.success(staffAuditRecordPage);
    }

    @GetMapping(value = "/staff-audit/check-statu")
    public ResultJson create(@RequestParam(value = "accountId") Integer accountId, @RequestParam(value = "enterpriseId",required = false)Integer enterpriseId, Account account) {
        if (null == account) {
            return ResultJson.fail("获取登录信息失败");
        }
        if (null == accountId){
            accountId = account.getId();
        }
        return staffAuditRecordService.checkStatu(accountId,null);
    }

    @RequestMapping("/staff-audit/delete")
    public ResultJson delete(@RequestParam(value = "id",required = true)Integer id,Account account) {
        StaffAuditRecord staffAuditRecord = staffAuditRecordService.getById(id);
        if (null == staffAuditRecord){
            return ResultJson.fail("认证不存在");
        }
        if (staffAuditRecord.getStatus() != 4){
            return ResultJson.fail("非法删除!");
        }
        staffAuditRecord.setIsDeleted(true);
        return ResultJson.success(staffAuditRecordService.updateById(staffAuditRecord));
    }

    private Map<Integer,String> enterpriseHandler(List<StaffAuditRecord> records){
        List<Integer> enterpriseIds = records.stream().map(StaffAuditRecord::getEnterpriseId).collect(Collectors.toList());
        EnterpriseQuery enterpriseQuery = new EnterpriseQuery();
        enterpriseQuery.setIdList(enterpriseIds);
        List<Enterprise> enterprises = enterpriseService.list(enterpriseQuery,null);
        if (!enterprises.isEmpty()){
            return enterprises.stream().collect(Collectors.toMap(Enterprise::getId, Enterprise::getName));
        }
        return null;
    }
    private Map<Integer,String> projectHandler(List<StaffAuditRecord> records){
        List<Integer> projectIds = records.stream().map(StaffAuditRecord::getProjectId).collect(Collectors.toList());
        ProjectQuery projectQuery = new ProjectQuery();
        projectQuery.setIds(projectIds);
        List<Project> projects = projectService.list(projectQuery,null);
        if (!projectIds.isEmpty()){
            return projects.stream().collect(Collectors.toMap(Project::getId, Project::getName));
        }
        return null;
    }
}

